package com.jjsj.web.controller.mall;

import com.jjsj.common.annotation.Log;
import com.jjsj.common.core.controller.BaseController;
import com.jjsj.common.core.domain.AjaxResult;
import com.jjsj.common.core.page.TableDataInfo;
import com.jjsj.common.enums.BusinessType;
import com.jjsj.common.utils.poi.ExcelUtil;
import com.jjsj.mall.entity.MallBanner;
import com.jjsj.mall.service.IMallBannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 首页广告Controller
 *
 * @author JJSJ
 * @date 2022-10-03
 */
@RestController
@RequestMapping("/mall/banner")
public class MallBannerController extends BaseController {
    @Autowired
    private IMallBannerService mallBannerService;

    /**
     * 查询首页广告列表
     */
    @PreAuthorize("@ss.hasPermi('mall:banner:list')")
    @GetMapping("/list")
    public TableDataInfo list(MallBanner mallBanner) {
        startPage();
        List<MallBanner> list = mallBannerService.selectMallBannerList(mallBanner);
        return getDataTable(list);
    }

    /**
     * 导出首页广告列表
     */
    @PreAuthorize("@ss.hasPermi('mall:banner:export')")
    @Log(title = "首页广告", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(MallBanner mallBanner) {
        List<MallBanner> list = mallBannerService.selectMallBannerList(mallBanner);
        ExcelUtil<MallBanner> util = new ExcelUtil<MallBanner>(MallBanner.class);
        return util.exportExcel(list, "banner");
    }

    /**
     * 获取首页广告详细信息
     */
    @PreAuthorize("@ss.hasPermi('mall:banner:query')")
    @GetMapping(value = "/{bannerId}")
    public AjaxResult getInfo(@PathVariable("bannerId") Long bannerId) {
        return AjaxResult.success(mallBannerService.selectMallBannerById(bannerId));
    }

    /**
     * 新增首页广告
     */
    @PreAuthorize("@ss.hasPermi('mall:banner:add')")
    @Log(title = "首页广告", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MallBanner mallBanner) {
        return toAjax(mallBannerService.insertMallBanner(mallBanner));
    }

    /**
     * 修改首页广告
     */
    @PreAuthorize("@ss.hasPermi('mall:banner:edit')")
    @Log(title = "首页广告", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MallBanner mallBanner) {
        return toAjax(mallBannerService.updateMallBanner(mallBanner));
    }

    /**
     * 删除首页广告
     */
    @PreAuthorize("@ss.hasPermi('mall:banner:remove')")
    @Log(title = "首页广告", businessType = BusinessType.DELETE)
    @DeleteMapping("/{bannerIds}")
    public AjaxResult remove(@PathVariable Long[] bannerIds) {
        return toAjax(mallBannerService.deleteMallBannerByIds(bannerIds));
    }
}
